package com.yic.module.device.controller.admin.inspectionRecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.device.controller.admin.inspectionRecord.vo.*;
import com.yic.module.device.dal.dataobject.inspectionRecord.InspectionRecordDO;
import com.yic.module.device.convert.inspectionRecord.InspectionRecordConvert;
import com.yic.module.device.service.inspectionRecord.InspectionRecordService;

@Tag(name = "管理后台 - 巡检/点检记录")
@RestController
@RequestMapping("/device/inspection-record")
@Validated
public class InspectionRecordController {

    @Resource
    private InspectionRecordService inspectionRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建巡检/点检记录")
    @PreAuthorize("@ss.hasPermission('device:inspection-record:create')")
    public CommonResult<Long> createInspectionRecord(@Valid @RequestBody InspectionRecordCreateReqVO createReqVO) {
        return success(inspectionRecordService.createInspectionRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新巡检/点检记录")
    @PreAuthorize("@ss.hasPermission('device:inspection-record:update')")
    public CommonResult<Boolean> updateInspectionRecord(@Valid @RequestBody InspectionRecordUpdateReqVO updateReqVO) {
        inspectionRecordService.updateInspectionRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除巡检/点检记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('device:inspection-record:delete')")
    public CommonResult<Boolean> deleteInspectionRecord(@RequestParam("id") Long id) {
        inspectionRecordService.deleteInspectionRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得巡检/点检记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('device:inspection-record:query')")
    public CommonResult<InspectionRecordRespVO> getInspectionRecord(@RequestParam("id") Long id) {
        InspectionRecordDO inspectionRecord = inspectionRecordService.getInspectionRecord(id);
        return success(InspectionRecordConvert.INSTANCE.convert(inspectionRecord));
    }

    @GetMapping("/list")
    @Operation(summary = "获得巡检/点检记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('device:inspection-record:query')")
    public CommonResult<List<InspectionRecordRespVO>> getInspectionRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<InspectionRecordDO> list = inspectionRecordService.getInspectionRecordList(ids);
        return success(InspectionRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得巡检/点检记录分页")
    @PreAuthorize("@ss.hasPermission('device:inspection-record:query')")
    public CommonResult<PageResult<InspectionRecordRespVO>> getInspectionRecordPage(@Valid InspectionRecordPageReqVO pageVO) {
        PageResult<InspectionRecordDO> pageResult = inspectionRecordService.getInspectionRecordPage(pageVO);
        return success(InspectionRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出巡检/点检记录 Excel")
    @PreAuthorize("@ss.hasPermission('device:inspection-record:export')")
    @OperateLog(type = EXPORT)
    public void exportInspectionRecordExcel(@Valid InspectionRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<InspectionRecordDO> list = inspectionRecordService.getInspectionRecordList(exportReqVO);
        // 导出 Excel
        List<InspectionRecordExcelVO> datas = InspectionRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "巡检/点检记录.xls", "数据", InspectionRecordExcelVO.class, datas);
    }

}
